package com.keyinong.parkin.fragment;

import java.util.ArrayList;

import org.apache.http.Header;
import org.json.JSONObject;

import com.example.parkin.R;
import com.keyinong.parkin.alipush.XiaoMiPushActivity;
import com.keyinong.parkin.home.adapter.AdListAdapter;
import com.keyinong.parkin.home.adapter.HomeServiceAdapter;
import com.keyinong.parkin.home.bean.AppAdsBean;
import com.keyinong.parkin.home.bean.ParkServiceBean;
import com.keyinong.parkin.home.meetingboofix.MeetingRoomListActivity;
import com.keyinong.parkin.home.notice.NoticeListActivity;
import com.keyinong.parkin.home.other.SelectAddressActivity;
import com.keyinong.parkin.home.parkingcharge.ParkingChargeActivity;
import com.keyinong.parkin.home.ticket.RepairTicketActivity;
import com.keyinong.parkin.internetmodel.MyJsonHttpResponseHander;
import com.keyinong.parkin.internetmodel.ToolModel;
import com.keyinong.parkin.util.ImageUtils;
import com.keyinong.parkin.util.ImageUtilsLoaderFile;
import com.keyinong.parkin.util.ListDataSave;
import com.keyinong.parkin.util.MarqueeTextView;
import com.keyinong.parkin.util.SystemBarTintManager;
import com.keyinong.parkin.util.Utility;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class HomeFragment extends Fragment {
	/** 更多 */
	private TextView tv_home_more;
	/** 公告 */
	private ImageView img_home_announcement;
	private ImageView img_home_head;
	private GridView gv_service;
//	private ImageView img_parkHead;
	/**园区名字*/
	private TextView tv_parkName;
	
	
	/**通知*/
	private MarqueeTextView tv_notice;
	/**广告图片*/
	private ListView list_ad;

	SharedPreferences sp;

	private ArrayList<ParkServiceBean> servicelist = new ArrayList<ParkServiceBean>();
	private ArrayList<AppAdsBean> adlist;

	ViewGroup group;


	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
			// 透明状态栏
			getActivity().getWindow().addFlags(
					WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
			// 透明导航栏
//			getActivity().getWindow().addFlags(
//					WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
		}

		return inflater.inflate(R.layout.fragment_home, null);

	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		SystemBarTintManager tintManager = new SystemBarTintManager(
				getActivity());
		tintManager.setStatusBarTintEnabled(true);
		tintManager.setStatusBarTintColor(getResources().getColor(
				R.color.white_1));
		super.onActivityCreated(savedInstanceState);

		initRes();
	}

	private void initRes() {
		tv_home_more = (TextView) getView().findViewById(R.id.tv_home_more);
		img_home_announcement = (ImageView) getView().findViewById(
				R.id.img_home_announcement);
//		img_parkHead = (ImageView) getView().findViewById(R.id.img_parkHead);
		img_home_announcement.setVisibility(View.GONE);
		img_home_head = (ImageView) getView().findViewById(R.id.img_home_head);
		gv_service = (GridView) getView().findViewById(R.id.gv_service);
		tv_notice=(MarqueeTextView) getView().findViewById(R.id.tv_notice);
		list_ad=(ListView) getView().findViewById(R.id.list_ad);
		tv_parkName=(TextView) getView().findViewById(R.id.tv_parkName);
		tv_notice.setText("欢迎使用ParkIn....................................");
		startres();
		addOnclick();
	}

	private void startres() {
		adlist = new ArrayList<AppAdsBean>();
		sp = getActivity().getSharedPreferences("spuser", Context.MODE_PRIVATE);
		String parkname=sp.getString("park_name", "");
//		tv_home_more.setText(parkname);
		tv_parkName.setText(parkname);
		if ("".equals(sp.getString("park_id", ""))) {
			intentActivity(SelectAddressActivity.class, 1);
		}
		// http://blog.csdn.net/shineflowers/article/details/44650425
		// imageloader

//		String app_logo = sp.getString("app_logo", "");
		String app_bg = sp.getString("park_bg", "");
		ImageUtilsLoaderFile.displayImage(getActivity(), app_bg, img_home_head,
				R.drawable.img_home);
//		ImageUtils.displayImage(getActivity(), app_logo, img_parkHead,
//				R.drawable.img_parklogo);
		ListDataSave service = new ListDataSave(getActivity(), "spservice");
		servicelist = service.getseviceList("servicelist");
		ListDataSave adslist = new ListDataSave(getActivity(), "adslist");
		adlist = adslist.getadList("adslist");
		// Log.e("lh", "servicelist=="+servicelist.toString());
		HomeServiceAdapter serviceadapter = new HomeServiceAdapter(
				getActivity(), servicelist);
		gv_service.setAdapter(serviceadapter);
		
		addAdList();
		getNotice();

	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == 1) {
			startres();
		}
	}

	// @Override
	// public void onStart() {
	// isLooper=true;
	// startres();
	// super.onStart();
	//
	// }

	private OnItemClickListener itemservice = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			switch (servicelist.get(position).getId()) {
			case 1:
				intentActivity(ParkingChargeActivity.class, 3);
				break;
			case 2:
				intentActivity(MeetingRoomListActivity.class, 4);
				break;
			case 3:
				intentActivity(RepairTicketActivity.class, 5);
				break;
			case 4:
				intentActivity(NoticeListActivity.class, 6);
				break;

			default:
				break;
			}

		}
	};

	private void addOnclick() {
		tv_home_more.setOnClickListener(homeOnclic);
		img_home_announcement.setOnClickListener(homeOnclic);
		// tv_parkingChange.setOnClickListener(homeOnclic);
		// tv_meetingbooFix.setOnClickListener(homeOnclic);
		// tv_rapairTicket.setOnClickListener(homeOnclic);
		gv_service.setOnItemClickListener(itemservice);
	}

	private OnClickListener homeOnclic = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			// 更多
			case R.id.tv_home_more:

				intentActivity(SelectAddressActivity.class, 1);
				break;
			// 公告
			case R.id.img_home_announcement:
				intentActivity(XiaoMiPushActivity.class, 2);
				break;
			// 停车交费
			// case R.id.tv_parkingChange:
			// intentActivity(ParkingChargeActivity.class);
			// break;
			// // 会议预订
			// case R.id.tv_meetingbooFix:
			// intentActivity(MeetingRoomListActivity.class);
			// break;
			// // 报修记录
			// case R.id.tv_rapairTicket:
			// intentActivity(RepairTicketActivity.class);
			// break;

			default:
				break;
			}

		}
	};

	private void addAdList(){
		ArrayList<Integer> adList=new ArrayList<Integer>();
		adList.add(R.drawable.img_ad_a);
		adList.add(R.drawable.img_ad_b);
		AdListAdapter adapter=new AdListAdapter(adList, getActivity());
		list_ad.setAdapter(adapter);
		Utility.setListViewHeightBasedOnChildren(list_ad);
	}

	private void intentActivity(Class<?> cls, int calseindex) {
		Intent intent = new Intent(getActivity(), cls);
		startActivityForResult(intent, calseindex);
		// startActivity(intent);
	}
	
	private void getNotice(){
		String parkid = sp.getString("park_id", "");
		ToolModel toolModel=new ToolModel(getActivity());
//		toolModel.getnotice(parkid, new MyJsonHttpResponseHander(getActivity()){
//			@Override
//			public void onSuccess(int statusCode, Header[] headers,
//					JSONObject response) {
//				super.onSuccess(statusCode, headers, response);
////				Log.e("lh", response.toString());
//			}
//			
//		});
	}
}
